﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;

namespace HR60_PanelSoft.View
{
	/// <summary>
	/// ProjTree右键菜单
	/// </summary>
	public class ProjTreeNodeMenu
	{
		private TreeView projTree = null;
		private ContextMenu contextMenu = new ContextMenu();
		private MenuItem addNet = new MenuItem("添加网络");
		private MenuItem delNet = new MenuItem("删除网络");
		private MenuItem addPanel = new MenuItem("添加面板");
		private MenuItem delPanel = new MenuItem("删除面板");
		private MenuItem addGroup = new MenuItem("添加分组");
		private MenuItem delGroup = new MenuItem("删除分组");
		private MenuItem addDevice = new MenuItem("添加设备");
		private MenuItem delDevice = new MenuItem("删除设备");
		private MenuItem addScene = new MenuItem("添加场景");
		private MenuItem delScene = new MenuItem("删除场景");
		public WindowNew windowNew = null;

		WindowMain win
		{
			get
			{
				return (WindowMain)projTree.FindForm();
			}
		}

		WindowNew winNew
		{
			get
			{
				if (windowNew == null)
				{
					windowNew = new WindowNew(win);
				}
				return windowNew;
			}
		}

		object nodeTag
		{
			get
			{
				return projTree.SelectedNode.Tag;
			}
		}

		public ProjTreeNodeMenu()
		{
			addNet.Click += new EventHandler(addNet_Click);
			delNet.Click += new EventHandler(delNet_Click);
			addPanel.Click += new EventHandler(addPanel_Click);
			delPanel.Click += new EventHandler(delPanel_Click);
			addGroup.Click += new EventHandler(addGroup_Click);
			delGroup.Click += new EventHandler(delGroup_Click);
			addDevice.Click += new EventHandler(addDevice_Click);
			delDevice.Click += new EventHandler(delDevice_Click);
			addScene.Click += new EventHandler(addScene_Click);
			delScene.Click += new EventHandler(delScene_Click);
		}

		// 绑定到ProjTreeView
		public void bindProjTree(TreeView projTree)
		{
			this.projTree = projTree;
			projTree.ContextMenu = contextMenu;

			// 弹出时判断显示哪些菜单项
			contextMenu.Popup += new EventHandler(contextMenu_Popup);

			// 保证按下鼠标右键后即选中
			projTree.MouseDown += new MouseEventHandler(projTree_MouseDown);
		}

		void projTree_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				TreeNode node = projTree.GetNodeAt(e.X, e.Y);
				if (node != null) projTree.SelectedNode = node;
			}
		}

		void contextMenu_Popup(object sender, EventArgs e)
		{
			show(projTree.SelectedNode);
		}

		// 根据选中项目显示不同的菜单
		void show(TreeNode treeNode)
		{
			Type tagType = treeNode.Tag == null ? null : treeNode.Tag.GetType();
			string nodeText = treeNode.Text;
			contextMenu.MenuItems.Clear();

			if (nodeText == "设备列表")
			{
				contextMenu.MenuItems.Add(addGroup);
			}
			else if (nodeText == "网络列表")
			{
				contextMenu.MenuItems.Add(addNet);
			}
			else if (nodeText == "场景列表")
			{
				contextMenu.MenuItems.Add(addScene);
			}
			else if (tagType == typeof(DeviceGroup))
			{
				contextMenu.MenuItems.Add(delGroup);
				contextMenu.MenuItems.Add(addDevice);
			}
			else if (tagType == typeof(Device))
			{
				contextMenu.MenuItems.Add(delDevice);
			}
			else if (tagType == typeof(HR60_PanelSoft.Model.Net))
			{
				contextMenu.MenuItems.Add(delNet);
				contextMenu.MenuItems.Add(addPanel);
			}
			else if (tagType == typeof(HR60_PanelSoft.Model.Panel))
			{
				contextMenu.MenuItems.Add(delPanel);
			}
			else if (tagType == typeof(Scene))
			{
				contextMenu.MenuItems.Add(delScene);
			}
		}

		#region "菜单项点击事件"

		void delScene_Click(object sender, EventArgs e)
		{
			win.doRemoveScene((Scene)nodeTag);
		}

		void addScene_Click(object sender, EventArgs e)
		{
			win.doCreateNewScene();
		}

		void delDevice_Click(object sender, EventArgs e)
		{
			win.doRemoveDevice((Device) nodeTag);
		}

		void addDevice_Click(object sender, EventArgs e)
		{
			winNew.showNewDevice();
		}

		void delGroup_Click(object sender, EventArgs e)
		{
			win.doRemoveDeviceGroup((DeviceGroup) nodeTag);
		}

		void addGroup_Click(object sender, EventArgs e)
		{
			win.doCreateNewDeviceGroup();
		}

		void delPanel_Click(object sender, EventArgs e)
		{
			win.doRemovePanel((HR60_PanelSoft.Model.Panel)nodeTag);
		}

		void addPanel_Click(object sender, EventArgs e)
		{
			winNew.showNewPanel();
		}

		void delNet_Click(object sender, EventArgs e)
		{
			win.doRemoveNet((HR60_PanelSoft.Model.Net)nodeTag);
		}

		void addNet_Click(object sender, EventArgs e)
		{
			win.doCreateNewNet();
		}

		#endregion

	}
}
